package com.zfxt.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zfxt.biz.mapper.UserMapper;
import com.zfxt.biz.model.User;
import com.zfxt.biz.service.IUserService;
import com.zfxt.common.HashUtils;
import com.zfxt.common.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author xuxing
 * @since 2018-08-30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private IUserService iUserService;

    @Autowired
    private MailUtils mailUtils;
    @Autowired
    private FileService fileService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertUser(User user) {
        user.setPasswd(HashUtils.encryPassword(user.getPasswd()));
        MultipartFile url=user.getAvatarFile();
        List<String> imgList = fileService.getImgPaths(Lists.newArrayList(user.getAvatarFile()));
        if (!imgList.isEmpty()) {
            user.setAvatar(imgList.get(0));
        }
        iUserService.save(user);
        mailUtils.registerNotify("房产中介注册信息",user.getEmail());
        return true;
    }

    public boolean enable(String key) {
        String mail= mailUtils.enable(key);
        if(mail.equals("")){
            return false;
        }
        User user=new User();
        user.setEmail(mail);
        return true;
    }
    /**
     * 用户名密码验证
     *
     * @param username
     * @param password
     * @return
     */
    public User auth(String username, String password) {
        User user = new User();
        user.setEmail(username);
        user.setPasswd(HashUtils.encryPassword(password));
        user.setEnable(1);
//        Map map =new HashMap();
//        map.put("email",username);
//        map.put("passwd",HashUtils.encryPassword(password));
//        map.put("enable",1);
//        List<User> list = iUserService.list(user);
//        if (!list.isEmpty()) {
//            return list.get(0);
//        }
        return user;
    }
}
